package com.edu.service.impl;

import com.edu.dao.CartsDao;
import com.edu.dao.OrdersDao;
import com.edu.dao.impl.CartsDaoImpl;
import com.edu.dao.impl.OrdersDaoImpl;
import com.edu.entity.OrderEntity;
import com.edu.service.OrdersService;

import java.util.LinkedList;

/**
 * com.edu.service.impl
 * 2024/7/11 10:07
 *
 * @author Fancy
 * @version 1.0
 */
public class OrdersServiceImpl implements OrdersService {
    OrdersDao ordersDao = new OrdersDaoImpl();
    CartsDao cartsDao = new CartsDaoImpl();

    @Override
    public int getOrderIdNoSettled(int userId) {
        return ordersDao.getOrderIdNoSettled(userId);
    }

    @Override
    public LinkedList<OrderEntity> getAllOrderByUserId(int userId) {
        LinkedList<OrderEntity> orders = ordersDao.getAllOrderByUserId(userId);
        for (OrderEntity order :
                orders) {
            order.setOrderItems(cartsDao.getCartItemByOrderId(order.getOrderId()));
        }
        return orders;
    }

    @Override
    public int updateOrderStatus(int orderId, String newStatus, String role) {
        if(role.equals("user") && (newStatus.equals("paid")||newStatus.equals("cancelled"))){
            //用户的操作只限对订单进行支付paid和删除cancelled
            return ordersDao.updateOrderStatus(orderId,newStatus);
        } else if (role.equals("admin") && (newStatus.equals("shipping")||newStatus.equals("completed"))) {
            //管理员从操作只限对订单的运输shipping，完成completed
            return ordersDao.updateOrderStatus(orderId,newStatus);
        }
        return 0;
    }

    @Override
    public int deleteOrder(int orderId) {
        return ordersDao.deleteOrder(orderId);
    }
}
